package view;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.GroceryListDao;

/**
 * The display frame and somewhat of a controller for the application
 * @author Lawrence Grass, Kevin Hedglin
 */
@SuppressWarnings("serial")
public class GroceryListFrame extends JFrame
							  implements ActionListener,
							  			 Observer{
	
	/**
	 * Sets the width of the application
	 */
	public static final int FRAME_WIDTH = 400;
	
	/**
	 * Sets the height of the application
	 */
	public static final int FRAME_HEIGHT = 640;
	
	private String user = "user";
	
	private String pass = "password";
	
	/**
	 * The dao
	 */
	private GroceryListDao dao = new GroceryListDao();
	
	/**
	 * Add item panel
	 */
	private AddItemPanel addItemPanel;
	
	/**
	 * Item list panel
	 */
	private ItemListPanel itemListPanel;
	
	/**
	 * History view panel
	 */
	private HistoryPanel historyPanel;
	
	/**
	 * Nutrition view panel
	 */
	private NutritionPanel nutritionPanel;
	
	private AddNutritionPanel addNutritionPanel;
	
	private JPanel cards;
	
	private CardLayout cl;
	
	/**
	 * Creates a new GroceryListFrame, which is the base frame for this app.
	 */
	public GroceryListFrame(){
		super("TCSS 445 Grocery List by Code3");
		this.setResizable(false);		
		this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		this.loginDialog();
		
		//sets up the view for displaying the list you're looking at
		itemListPanel = new ItemListPanel(this, dao);
		
		//add the items panel
		addItemPanel = new AddItemPanel(this, dao);
		
		//add the history panel
		historyPanel = new HistoryPanel(this, dao);
		
		nutritionPanel = new NutritionPanel(this, dao);
		
		addNutritionPanel = new AddNutritionPanel(this, dao);
		
		cl = new CardLayout();
		cards = new JPanel();
		cards.setLayout(cl);
		cards.add(itemListPanel, "list");
		cards.add(addItemPanel, "add");
		cards.add(historyPanel, "history");
		cards.add(nutritionPanel, "nutrition");
		cards.add(addNutritionPanel, "add nutrition");
		this.add(cards);
		
	}
	
	public void loginDialog(){
		JPanel login = new JPanel();
		JLabel userlabel = new JLabel("User Name: ");
		JTextField username = new JTextField(20);
		login.add(userlabel);
		login.add(username);
		JLabel pswrd = new JLabel("Password: ");
		JTextField password = new JTextField(20);
		login.add(pswrd);
		login.add(password);
		String[] options = new String[]{"OK", "Cancel"};
		int option = JOptionPane.showOptionDialog(null, login, "Please enter a valid login or hit Cancel to close.",
				JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
				null, options, options[0]);		
		if(option == 0){
			user = username.getText();
			pass = password.getText();
		} else {
			System.exit(0);
		}
		
		if(dao.checkConnection(user, pass)){
			
			dao = new GroceryListDao(user, pass);
		} else {
			JOptionPane.showMessageDialog(this,
				    "Not a valid login, please try again.");
			loginDialog();
		}
	}
	
	public void goBack(){
		cl.previous(cards);
	}
	
	public void goMain(){
		cl.first(cards);
	}
	
	public void refreshCards(){
		itemListPanel.refreshItemsList();
		historyPanel.refreshItemsList();
		nutritionPanel.refreshItemsList();
		cl.next(cards);
		cl.previous(cards);
		this.revalidate();
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		JButton clickedButton = (JButton) event.getSource();
		if(clickedButton.getText().equals("Add Item")){
			cl.next(cards);
		}else if(clickedButton.getText().equals("View History")){
			refreshCards();
			cl.show(cards, "history");
		}else if(clickedButton.getText().equals("Nutrition Info")){
			refreshCards();
			cl.show(cards, "nutrition");
		}else if(clickedButton.getText().equals("Add Nutrition")){
			refreshCards();
			cl.show(cards, "add nutrition");
		}else if(clickedButton.getText().equals("Print")){
			try {
				dao.clearGroceryList();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			refreshCards();
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		loginDialog();
		
	}
}
